package abstractComponent;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import concreteComponents.ConcatenarValores;
import concreteComponents.GeneradorLeer;
import concreteComponents.GeneradorModificar;
import concreteComponents.Multiplicador;
import concreteComponents.Sumador;

import channel.Channel;

abstract public class Transformador {

	static Map<String, Class<? extends Transformador>> map = new HashMap<String, Class<? extends Transformador>>();

	public static void main(String[] args) throws InstantiationException,
			IllegalAccessException {
		Set<Channel<Serializable>> channelsS = new HashSet<Channel<Serializable>>();
		List<Channel<Integer>> channelsR = new ArrayList<Channel<Integer>>();
		map.put("Sumador", Sumador.class);
		map.put("Multiplicador", Multiplicador.class);
		map.put("GeneradorModificar", GeneradorModificar.class);
		map.put("GeneradorLeer", GeneradorLeer.class);
		map.put("ConcatenarValores", ConcatenarValores.class);
		Transformador instance = map.get(args[0]).newInstance();
		String flag="";
		for (int n = 1;n < args.length;n++) {
			
			if(args[n].equals("-cs")){
				flag = "cs";
				continue;
			} 
			
			if(args[n].equals("-cr")){
					flag = "cr";
					continue;
			}
			
			if(flag.equals("cs")){
				int i = Integer.parseInt(args[n]);
				channelsS.add(new Channel<Serializable>(i));
			}
			else{
				int i = Integer.parseInt(args[n]);
				channelsR.add(channelsR.size(), new Channel<Integer>(i));
			}
		}
		
		while (true) {
			ArrayList<Integer> array = new ArrayList<Integer>();
			for (Channel<Integer> channel : channelsR){
				array.add(array.size(), channel.receive());
			}
			
			Serializable toSend = instance.accion(array);
			for (Channel<Serializable> channel : channelsS) {
				channel.send(toSend);
			}
		}
	}

	public abstract Serializable accion(List<Integer> ints);
}

